const SpiderDouyu = require('../../live_spider_client/spider/SpiderDouYu');
const _ = require('lodash');

module.exports = {
  async douyu(url) {
    const res = await SpiderDouyu.get(url,
      {dataType: 'text', headers: SpiderDouyu.headers('')});

    //缩小范围
    const tagList = res.data.match(/tag_list">.+?<\/div/gs);
    // console.log(1);
    if (!tagList || !tagList[0])
      return [];

    //提取a元素
    const list = tagList[0].match(/<a.+?data-rk.+?\/a/gs);
    // console.log(2);
    if (!list || list.length === 0)
      return [];

    //提取要点
    return _.keyBy(list.map(html => {
      // console.log({html});
      const match = html.match(/data-rk="(\d+_\d+).+?>\s*(.+)\s*</s);
      const name = match[2].trim();
      const isAll = name === '全部';
      return {key: match[1].trim(), name, isAll};
    }), 'key');

  },
};